package com.beifu.manager.api.controller.wash;

import com.beifu.manager.api.common.PageInfo;
import com.beifu.manager.api.domain.warehouse.ImgBase64;
import com.beifu.manager.api.domain.wash.*;
import com.beifu.manager.service.assetoutservice.AssetOutService;
import com.beifu.manager.vo.assetout.AssetsOutListApiVo;
import com.utils.ResponseHelp;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@ResponseBody
@RequestMapping(value="/mobileserve/wash/api", produces = "application/json;charset=UTF-8")
//@RequestMapping(value="/wash/api", produces = "application/json;charset=UTF-8")
public class WashApiController {
    @Autowired
    private AssetOutService assetOutService;

	/**
	 * 卧具洗涤单列表
	 * @param reqMap
	 * @return
	 */
	@RequestMapping(value="/getAssetsWashList", method= RequestMethod.POST)
    public String getAssetsWashList(@RequestBody Map<String, Object> reqMap) {
        // 接收pub和in区域的请求数据
        JSONObject jsonPub = JSONObject.fromObject(reqMap.get("pub"));
        JSONObject jsonIn = JSONObject.fromObject(reqMap.get("in"));
        // 获取具体值，先这样，
        String userId = jsonPub.get("userId").toString();
        String devID = jsonPub.get("devID").toString();
        String timestamp = jsonPub.get("timestamp").toString();
        // 接口参数
        Integer userIdInt = Integer.valueOf(userId);

        int pageIndex = 1;
        int pageNumber = 10;

        try{
            if (null == jsonIn.get("pageIndex")) {
                return ResponseHelp.responseApiErrorText("1111", "pageIndex不能为空！");
            }
            if (null == jsonIn.get("pageNumber")) {
                return ResponseHelp.responseApiErrorText("1112", "pageNumber不能为空！");
            }
            String pageIndexStr = jsonIn.get("pageIndex").toString();
            String pageNumberStr = jsonIn.get("pageNumber").toString();
            pageIndex = Integer.decode(pageIndexStr);
            pageNumber = Integer.decode(pageNumberStr);
        } catch (NullPointerException e) {
            e.printStackTrace();
            return ResponseHelp.responseApiErrorText("1113","请输入正确的分页参数！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseHelp.responseApiErrorText("1113","请输入正确的分页参数！");
        }


        PageInfo pageInfo = assetOutService.getAssetsWashList(userIdInt, pageIndex, pageNumber);
        return  ResponseHelp.responseApiSuccessText(pageInfo);
	}
	
	/**
     * 卧具洗涤单详情
     */
    @RequestMapping(value="/getAssetsWashDetail")
    public String getAssetsWashDetail(@RequestBody Map<String, Object> reqMap) {
        // 接收pub和in区域的请求数据
        JSONObject jsonPub = JSONObject.fromObject(reqMap.get("pub"));
        JSONObject jsonIn = JSONObject.fromObject(reqMap.get("in"));
        // 获取具体值，先这样，
        // TODO 待优化
        String userId = "";
        String devID = "";
        String timestamp = "";
        if (!JSONUtils.isNull(jsonPub)) {
			userId = (String) jsonPub.get("userId");
			devID = (String) jsonPub.get("devID");
			timestamp = (String) jsonPub.get("timestamp");
		}
        
        if(JSONUtils.isNull(jsonIn)){
        	return ResponseHelp.responseApiErrorText("2222", "传入参数不能为空！");
        }
        if (null == jsonIn.get("assetWashId")) {
            return ResponseHelp.responseApiErrorText("2222", "卧具洗涤单ID为空！");
        }
        String assetWashIdStr = jsonIn.get("assetWashId").toString();
        int assetWashId = Integer.valueOf(assetWashIdStr);
        AssetsWashDetailResponse assetsWashDetail = assetOutService.getAssetsWashDetail(assetWashId);

        return ResponseHelp.responseApiSuccessText(assetsWashDetail);
    }



    /**
     * 洗涤明细 washDetailSubDetail
     */
    @RequestMapping(value="/getWashDetailSubDetail")
    public String getWashDetailSubDetail(@RequestBody Map<String, Object> reqMap) {
    	// 接收pub和in区域的请求数据
        JSONObject jsonPub = JSONObject.fromObject(reqMap.get("pub"));
        JSONObject jsonIn = JSONObject.fromObject(reqMap.get("in"));
        // 获取具体值，先这样，
        // TODO 待优化
        String userId = "";
        String devID = "";
        String timestamp = "";
        if (!JSONUtils.isNull(jsonPub)) {
			userId = (String) jsonPub.get("userId");
			devID = (String) jsonPub.get("devID");
			timestamp = (String) jsonPub.get("timestamp");
		}
        
        if(JSONUtils.isNull(jsonIn)){
        	return ResponseHelp.responseApiErrorText("2222", "传入参数不能为空！");
        }
		if (null == jsonIn.get("assetWashId")) {
            return ResponseHelp.responseApiErrorText("2222", "卧具洗涤单ID为空！");
        }
        if (null == jsonIn.get("trainUnitId")) {
        	return ResponseHelp.responseApiErrorText("3333", "车厢id为空！");
        }
        
        String assetWashId = jsonIn.get("assetWashId").toString();
        String trainUnitId = StringUtils.EMPTY;
        try {
            trainUnitId = jsonIn.get("trainUnitId").toString();
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("类型转化异常");
        }
        WashDetailSummary washDetailSubDetail =null;
        try {
            washDetailSubDetail = assetOutService.getWashDetailSubDetail(assetWashId, trainUnitId);
        }catch (RuntimeException e) {
        e.printStackTrace();
        return ResponseHelp.responseApiErrorText("111113",e.getMessage());
        } catch (Exception e) {
        e.printStackTrace();
        return ResponseHelp.responseApiErrorText("111114","系统异常！");
    }

    	 return ResponseHelp.responseApiSuccessText(washDetailSubDetail);
    }

    /**
     * 上传扫描 明细  uploadwashscanDetail
     */
    @RequestMapping(value="/uploadwashscanDetail")
    public String uploadwashscanDetail(@RequestBody Map<String, Object> reqMap) {
        // 接收pub和in区域的请求数据
        JSONObject jsonPub = JSONObject.fromObject(reqMap.get("pub"));
        JSONObject jsonIn = JSONObject.fromObject(reqMap.get("in"));
        // 获取具体值，先这样，
        // TODO 待优化
        String userId = "";
        String devID = "";
        String timestamp = "";
        if (!JSONUtils.isNull(jsonPub)) {
            userId = (String) jsonPub.get("userId");
            devID = (String) jsonPub.get("devID");
            timestamp = (String) jsonPub.get("timestamp");
        }

        if(JSONUtils.isNull(jsonIn)){
            return ResponseHelp.responseApiErrorText("2222", "传入参数不能为空！");
        }
        if (null == jsonIn.get("assetsWashId")) {
            return ResponseHelp.responseApiErrorText("2222", "卧具洗涤单ID为空！");
        }
        if (null == jsonIn.get("rfidList")) {
            return ResponseHelp.responseApiErrorText("3333", "rfidList为空！");
        }

        String assetWashId = jsonIn.get("assetsWashId").toString();
        String rfIdOnlyListStr = jsonIn.get("rfidList").toString();

        JSONArray json = JSONArray.fromObject(rfIdOnlyListStr);
        List<RfIdOnly> rfIdOnlyList= (List<RfIdOnly>)JSONArray.toCollection(json, RfIdOnly.class);

        try{
            assetOutService.uploadwashscanDetail(rfIdOnlyList,userId,devID,assetWashId,timestamp);
        } catch (RuntimeException e){
            String message = e.getMessage();
            return ResponseHelp.responseApiErrorText("400007",message);
        } catch (Exception e){
            e.printStackTrace();
            return ResponseHelp.responseApiErrorText("400006","请求出错请联系管理员");
        }

        return ResponseHelp.responseApiSuccessText("");
    }

}
